﻿using HZYProject.Core.Repositories;
using HZYProject.Domain;
using HZYProject.Service;
using HZYProject.Service.ClassServices;
using HZYProject.Service.CollegeServices;
using HZYProject.Web.Models;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace HZYProject.Web.Controllers
{
    /// <summary>
    /// 班级管理
    /// </summary>
    [Route("Class")]
    public class ClassController : Controller
    {
        private readonly IRepository<User, Guid> _userRepository;
        private readonly IRepository<Colleges, Guid> _collegesRepository;
        private readonly IRepository<Class, Guid> _repository;
        private readonly IClassService _classService;

        public ClassController(IRepository<Class, Guid> repository, IClassService classService, IRepository<User, Guid> userRepository, IRepository<Colleges, Guid> collegesRepository)
        {
            _userRepository = userRepository;
            _repository = repository;
            _classService = classService;
            _collegesRepository = collegesRepository;
        }

        #region 首页分页查询
        [HttpGet("")]
        public IActionResult Index()
        {
            var list = _collegesRepository.GetAllList();
            return View(list);
        }
        [HttpPost("")]
        public async Task<IActionResult> Index(UIClass c)
        {
            try
            {
                GetClassInput input = new GetClassInput { ID = c.ClassID, Name = c.Name, CID = c.CollegesID };
                var data = await _classService.GetPaginatedResult(input);
                return Json(new { code = 0, msg = "", data = data.Data, count = data.TotalCount });
            }
            catch (Exception ex)
            {
                return Json(new { code = 1, msg = "查询出错:" + ex.Message, data = new List<UIClass>(), count = 0 });
            }
        }
        #endregion

        #region 添加
        [HttpGet("add")]
        public IActionResult Add()
        {
            return View();
        }
        [HttpPost("add")]
        public IActionResult Add(UIClass cla)
        {
            try
            {
                var n = User.Identity.Name;
                var user = _userRepository.FirstOrDefault(x => x.UserName == n);
                Class c = new Class();
                c.ClassID = Guid.NewGuid();
                c.Name = cla.Name;
                c.CollegesID = user.CollegesID;
                c.Status = cla.Status;
                c.CreateTime = DateTime.Now;
                _repository.Insert(c);
                return Json(new { success = true, msg = "添加成功" });
            }
            catch (Exception ex)
            {
                return Json(new { success = false, msg = ex.Message });
            }
        }
        #endregion

        #region 修改
        [HttpGet("edit")]
        public IActionResult Edit(Guid id)
        {
            var obj = _repository.FirstOrDefault(x => x.ClassID == id);
            return View(obj);
        }
        [HttpPost("edit")]
        public IActionResult Edit(UIClass cla)
        {
            try
            {
                if (ModelState.IsValid)//obj在形成对象时，是否全部符合注解中的规定
                {
                    var obj = _repository.FirstOrDefault(x => x.ClassID == cla.ClassID);
                    obj.Name = cla.Name;
                    obj.Status = cla.Status;
                    _repository.Update(obj);
                    return Json(new { success = true, msg = "修改成功" });
                }
                else
                {
                    System.Text.StringBuilder sb = new System.Text.StringBuilder();
                    int i = 1;
                    foreach (var v in ModelState.Values)
                    {
                        foreach (var p in v.Errors)
                        {
                            sb.AppendFormat("{0}-{1};", i++, p.ErrorMessage);
                        }
                    }
                    return Json(new { success = false, msg = "服务端验证未通过," + sb });
                }
            }
            catch (Exception ex)
            {
                return Json(new { success = false, msg = ex.Message });
            }

        }
        #endregion

        #region 删除
        [HttpPost("remove/{id}")]
        public IActionResult Del(Guid id)
        {
            try
            {
                var obj = _repository.FirstOrDefault(x => x.ClassID == id);
                _repository.Delete(obj);
                return Json(new { success = true, msg = "删除成功" });
            }
            catch (Exception ex)
            {
                return Json(new { success = false, msg = ex.Message });
            }
        }
        //[HttpPost("batchRemove/{id}")]
        //public IActionResult BatchRemove(string ids)
        //{
        //    try
        //    {

        //        var obj = _repository.FirstOrDefault(x => x.ClassID == ids);
        //        _repository.Delete(obj);
        //        return Json(new { success = true, msg = "删除成功" });
        //    }
        //    catch (Exception ex)
        //    {
        //        return Json(new { success = false, msg = ex.Message });
        //    }
        //}
        #endregion
    }
}
